package com.easyexcle.demo.easyExcel;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.easyexcle.demo.easyExcel.style.EasyExcelTitleHandler;
import com.easyexcle.demo.pojo.CustomMerge;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: lkz
 * @Title: TestCustomMerge
 * @Description: TODO
 * @Date: 2023/8/2 10:58
 */

public class TestCustomMerge {

    /**
     * 测试表头组合
     */
    @Test
    public void test(){
        try {

            String fileName =  "D:\\" + System.currentTimeMillis() + ".xlsx";

            ExcelWriter excelWriter = null;
            try {


                excelWriter = EasyExcel.write(fileName, CustomMerge.class).build();

                WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.TRUE)
                        .registerWriteHandler(new EasyExcelTitleHandler(DateUtil.format(new Date(),"yyyy"),"")).build();
                excelWriter.write(initData(), writeSheet);
            } finally {
                // 千万别忘记finish 会帮忙关闭流
                if (excelWriter != null) {
                    excelWriter.finish();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public List<CustomMerge> initData(){
        List data=new ArrayList();
        for(int i=0;i<10;i++){
            CustomMerge customMerge = new CustomMerge();
            customMerge.setIndex(i);
            customMerge.setIsTest(i/2==0?"是":"否");
            customMerge.setIsPD(i/2==0?"是":"否");
            customMerge.setIsMD(i/2==0?"是":"否");
            customMerge.setProjectName("项目名称"+i);
            customMerge.setProjectType("类型:"+i);
            customMerge.setSup("年度总结"+i);
            data.add(customMerge);
        }

        return data;
    }

}
